package com.doufeng.dispatch.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doufeng.dispatch.entity.Task;
import com.doufeng.dispatch.mapper.TaskMapper;
import com.doufeng.dispatch.service.TaskService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.data.geo.Point;

import java.awt.*;

@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Long createTask(Long orderId, Double lng, Double lat) {
        // 1. 写任务表
        Task task = new Task();
        task.setOrderId(orderId);
        task.setCreateTime(java.time.LocalDateTime.now());
        task.setPayTime(java.time.LocalDateTime.now());
        task.setStatus(0); // WAIT
        this.save(task);

        // 2. Geo 索引
        stringRedisTemplate.opsForGeo()
                .add("task:geo", new Point(lng, lat), task.getId().toString());
        return task.getId();
    }
}
